<template>
    <div class="app-container">
        <div class="Search">
            <!--工具条-->
            <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
                <el-form :inline="true" :model="query">

                    <el-form-item v-has="'PLATFROM_TENANT_QUERY'">
                        <el-cascader ref="tenantTree" placeholder="所属租户" :props="tenantProps" :options="tenantOptions"
                            @change="selectTenant" clearable collapse-tags filterable></el-cascader>
                    </el-form-item>
                    <el-form-item>
                        <el-cascader ref="organTree" placeholder="所属组织" :props="props" :options="organOptions"
                            @change="selectOrgan" collapse-tags clearable filterable></el-cascader>
                    </el-form-item>
                    <el-form-item>
                        <el-input v-model="query.params.couponName" placeholder="优惠券名称" />
                    </el-form-item>
                    <el-form-item>
                        <el-select v-model="query.params.costAllocation" placeholder="成本分摊">
                            <el-option v-for="item in query.couponCostAllocationOptions" :key="item.value"
                                :label="item.label" :value="item.value">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <div v-show="showAdvenceQuery">
                        <el-form-item>
                            <el-date-picker v-model="query.params.couponGiveEndDate" type="daterange" range-separator="至"
                                start-placeholder="领券/发券结束时间起" end-placeholder="领券/发券结束时间止">
                            </el-date-picker>
                        </el-form-item>
                        <el-form-item>
                            <el-date-picker v-model="query.params.couponGiveStartDate" type="daterange" range-separator="至"
                                start-placeholder="领券/发券开始时间起" end-placeholder="领券/发券开始时间止">
                            </el-date-picker>
                        </el-form-item>
                        <el-form-item>
                            <el-date-picker v-model="query.params.couponUseStartDate" type="daterange" range-separator="至"
                                start-placeholder="用券-开始时间起" end-placeholder="用券-开始时间止">
                            </el-date-picker>
                        </el-form-item>
                        <el-form-item>
                            <el-date-picker v-model="query.params.couponUseEndDate" type="daterange" range-separator="至"
                                start-placeholder="用券-结束时间起" end-placeholder="用券-结束时间止">
                            </el-date-picker>
                        </el-form-item>
                        <el-form-item>
                            <el-select v-model="query.params.couponApplySource" placeholder="适用渠道">
                                <el-option v-for="item in query.couponApplySourceOptions" :key="item.value"
                                    :label="item.label" :value="item.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item>
                            <el-select v-model="query.params.couponType" placeholder="类型">
                                <el-option v-for="item in query.couponTypeOptions" :key="item.value" :label="item.label"
                                    :value="item.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item>
                            <el-select v-model="query.params.couponUseType" placeholder="用券方式">
                                <el-option v-for="item in query.couponUseTypeOptions" :key="item.value" :label="item.label"
                                    :value="item.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item>
                            <el-select v-model="query.params.couponUserType" placeholder="优惠券使用用户类型">
                                <el-option v-for="item in query.couponUserTypeOptions" :key="item.value" :label="item.label"
                                    :value="item.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item>
                            <el-select v-model="query.params.couponUsingRange" placeholder="优惠券使用范围">
                                <el-option v-for="item in query.couponUsingRangeOptions" :key="item.value"
                                    :label="item.label" :value="item.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item>
                            <el-select v-model="query.params.couponStatus" placeholder="0：草稿；1：启用；2：禁用；3：已过期；">
                                <el-option v-for="item in query.couponStatusOptions" :key="item.value" :label="item.label"
                                    :value="item.value">
                                </el-option>
                            </el-select>
                        </el-form-item>

                        <el-form-item>
                            <el-select v-model="query.params.platformId" placeholder="平台类型: 参考枚举-客户端类型">
                                <el-option v-for="item in query.platformOptions" :key="item.value" :label="item.label"
                                    :value="item.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item>
                            <el-cascader v-model="query.params.storeId" ref="storeForm" placeholder="店铺编号"
                                :props="storeProps" :options="storeOptions" @change="chooseStoreForm(form)"
                                style="width:90%" clearable collapse-tags filterable></el-cascader>
                        </el-form-item>
                    </div>


                    <el-form-item>
                        <el-button type="primary" icon="el-icon-search" size="mini" @click="fetchData">查询</el-button>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="success" icon="el-icon-plus" size="mini" @click="handleAdd">发布优惠券</el-button>
                    </el-form-item>
                    <el-form-item>
                        <el-link type="primary" @click="handleShowAdvenceQuery">高级查询</el-link>
                    </el-form-item>
                </el-form>
            </el-col>
        </div>

        <div class="block">
            <el-table v-loading="listLoading" :data="list" element-loading-text="Loading" border fit highlight-current-row>
                <el-table-column align="center" label="序号" width="95">
                    <template slot-scope="scope">
                        {{ scope.$index }}
                    </template>
                </el-table-column>
                <el-table-column label="名称" width="110">
                    <template slot-scope="scope">
                        {{ scope.row.couponName }}
                    </template>
                </el-table-column>
                <el-table-column label="成本分摊" width="110">
                    <template slot-scope="scope">
                        {{ scope.row.costAllocation == 1 ? '平台分摊' : '店铺分摊' }}
                    </template>
                </el-table-column>
                <el-table-column label="优惠券面额" width="110">
                    <template slot-scope="scope">
                        {{ scope.row.couponAmount }}
                    </template>
                </el-table-column>
                <el-table-column label="适用渠道" width="110">
                    <template slot-scope="scope">
                        {{ scope.row.couponApplySource == 1 ? '全部渠道' : '门店' }}
                    </template>
                </el-table-column>
                <el-table-column label="使用门槛" width="110">
                    <template slot-scope="scope">
                        {{ scope.row.couponCondition }}
                    </template>
                </el-table-column>
                <el-table-column label="抵扣金额" width="110">
                    <template slot-scope="scope">
                        {{ scope.row.couponDiscount }}
                    </template>
                </el-table-column>
                <el-table-column label="领券/发券时间-结束时间" width="110">
                    <template slot-scope="scope">
                        {{ scope.row.couponGiveEndDate }}
                    </template>
                </el-table-column>
                <el-table-column label="领券/发券时间-开始时间" width="110">
                    <template slot-scope="scope">
                        {{ scope.row.couponGiveStartDate }}
                    </template>
                </el-table-column>
                <el-table-column label="描述" width="110">
                    <template slot-scope="scope">
                        {{ scope.row.couponInfo }}
                    </template>
                </el-table-column>
                <el-table-column label="优惠最高金额" width="110">
                    <template slot-scope="scope">
                        {{ scope.row.couponMaxAmount }}
                    </template>
                </el-table-column>

                <el-table-column label="发放张数,最大值=100000，修改优惠券总量时只能增加不能减少，请谨慎设置;" width="110">
                    <template slot-scope="scope">
                        {{ scope.row.couponNum }}
                    </template>
                </el-table-column>
                <el-table-column label="0：草稿；1：启用；2：禁用；3：已过期；" width="110">
                    <template slot-scope="scope">
                        {{ scope.row.couponStatus }}
                    </template>
                </el-table-column>
                <el-table-column label="类型" width="110">
                    <template slot-scope="scope">
                        {{ scope.row.couponType == 1 ? '满减券' : '运费券' }}
                    </template>
                </el-table-column>
                <el-table-column label="用券-开始时间" width="110">
                    <template slot-scope="scope">
                        {{ scope.row.couponUseStartDate }}
                    </template>
                </el-table-column>
                <el-table-column label="用券-结束时间" width="110">
                    <template slot-scope="scope">
                        {{ scope.row.couponUseEndDate }}
                    </template>
                </el-table-column>
                <el-table-column label="用券-效期,x天h时m分s秒，如：1天0时0分" width="110">
                    <template slot-scope="scope">
                        {{ scope.row.couponUseExpirationTimestamp }}
                    </template>
                </el-table-column>
                <el-table-column label="用券方式" width="110">
                    <template slot-scope="scope">
                        {{ scope.row.couponUseType == 1 ? '固定时间' : '领取后生效' }}
                    </template>
                </el-table-column>
                <el-table-column label="已发放张数, 系统默认值=0" width="110">
                    <template slot-scope="scope">
                        {{ scope.row.couponUsed }}
                    </template>
                </el-table-column>
                <el-table-column label="每人限领张数,0表示无限制，最大值=发放张数;" width="110">
                    <template slot-scope="scope">
                        {{ scope.row.couponUserNum }}
                    </template>
                </el-table-column>
                <el-table-column label="优惠券使用用户类型" width="110">
                    <template slot-scope="scope">
                        {{ scope.row.couponUserType }}
                    </template>
                </el-table-column>
                <el-table-column label="优惠券使用范围：1. 平台通用类；2. 店铺通用类;3. 品类通用类4. SKU使用类" width="110">
                    <template slot-scope="scope">
                        {{ scope.row.couponUsingRange }}
                    </template>
                </el-table-column>
                <el-table-column label="优惠券使用需要的满足条件" width="110">
                    <template slot-scope="scope">
                        {{ scope.row.meetPrice }}
                    </template>
                </el-table-column>
                <el-table-column label="平台类型: 参考枚举-客户端类型" width="110">
                    <template slot-scope="scope">
                        {{ scope.row.platformId }}
                    </template>
                </el-table-column>

                <el-table-column label="审核人" width="110">
                    <template slot-scope="scope">
                        {{ scope.row.reviewer }}
                    </template>
                </el-table-column>
                <el-table-column label="排序" width="110">
                    <template slot-scope="scope">
                        {{ scope.row.sorted }}
                    </template>
                </el-table-column>
                <el-table-column label="状态" width="110">
                    <template slot-scope="scope">
                        {{ scope.row.status }}
                    </template>
                </el-table-column>
                <el-table-column label="应用于店铺" width="110">
                    <template slot-scope="scope">
                        {{ scope.row.storeId }}
                    </template>
                </el-table-column>
                <el-table-column label="所属租户" width="110">
                    <template slot-scope="scope">
                        {{ scope.row.tenantId }}
                    </template>
                </el-table-column>
                <el-table-column label="所属组织" width="110">
                    <template slot-scope="scope">
                        {{ scope.row.organId }}
                    </template>
                </el-table-column>
                <el-table-column label="创建者" width="110">
                    <template slot-scope="scope">
                        {{ scope.row.createBy }}
                    </template>
                </el-table-column>
                <el-table-column label="创建时间" width="110">
                    <template slot-scope="scope">
                        {{ scope.row.createDate }}
                    </template>
                </el-table-column>
                <el-table-column label="更新者" width="110">
                    <template slot-scope="scope">
                        {{ scope.row.updateBy }}
                    </template>
                </el-table-column>
                <el-table-column label="更新时间" width="110">
                    <template slot-scope="scope">
                        {{ scope.row.updateDate }}
                    </template>
                </el-table-column>
                <el-table-column label="备注" width="110">
                    <template slot-scope="scope">
                        {{ scope.row.remark }}
                    </template>
                </el-table-column>
                <el-table-column align="center" prop="created_at" label="操作" width="200">
                    <template slot-scope="scope">
                        <el-button type="primary" icon="el-icon-search" size="mini"
                            @click="handleEdit(scope.$index, scope.row)">修改</el-button>
                        <el-button type="danger" icon="el-icon-plus" size="mini"
                            @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>

        <div class="block">
            <el-pagination :current-page="currentPage" :page-sizes="[10, 20, 30, 40]" :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="handleSizeChange"
                @current-change="handleCurrentChange" />
        </div>

        <div class="block">
            <!--弹框-->
            <el-dialog :title="titleMap[dialogStatus]" :visible.sync="dialogFormVisible">
                <el-form :model="form" :rules="CouponRules" ref="CouponForm">
                    <el-form-item label="成本分摊 1:平台分摊，2店铺分摊" required prop="costAllocation">
                        <el-select v-model="form.costAllocation" placeholder="成本分摊 1:平台分摊，2店铺分摊">
                            <el-option v-for="item in query.options" :key="item.value" :label="item.label"
                                :value="item.value">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="适用渠道,1：全部渠道；2：门店" required prop="couponApplySource">
                        <el-select v-model="form.couponApplySource" placeholder="适用渠道,1：全部渠道；2：门店">
                            <el-option v-for="item in query.options" :key="item.value" :label="item.label"
                                :value="item.value">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="使用门槛" required prop="couponCondition">
                        <el-select v-model="form.couponCondition" placeholder="使用门槛">
                            <el-option v-for="item in query.options" :key="item.value" :label="item.label"
                                :value="item.value">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="抵扣金额" required prop="couponDiscount">
                        <el-select v-model="form.couponDiscount" placeholder="抵扣金额">
                            <el-option v-for="item in query.options" :key="item.value" :label="item.label"
                                :value="item.value">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="领券/发券时间-结束时间" required prop="couponGiveEndDate">
                        <el-date-picker v-model="form.couponGiveEndDate" type="date" placeholder="领券/发券时间-结束时间">
                        </el-date-picker>
                    </el-form-item>
                    <el-form-item label="领券/发券时间-开始时间" required prop="couponGiveStartDate">
                        <el-date-picker v-model="form.couponGiveStartDate" type="date" placeholder="领券/发券时间-开始时间">
                        </el-date-picker>
                    </el-form-item>
                    <el-form-item label="描述" required prop="couponInfo">
                        <el-input v-model="form.couponInfo" placeholder="描述" auto-complete="off" />
                    </el-form-item>
                    <el-form-item label="名称, 满100减20" required prop="couponName">
                        <el-input v-model="form.couponName" placeholder="名称, 满100减20" auto-complete="off" />
                    </el-form-item>
                    <el-form-item label="发放张数,最大值=100000，修改优惠券总量时只能增加不能减少，请谨慎设置;" required prop="couponNum">
                        <el-select v-model="form.couponNum" placeholder="发放张数,最大值=100000，修改优惠券总量时只能增加不能减少，请谨慎设置;">
                            <el-option v-for="item in query.options" :key="item.value" :label="item.label"
                                :value="item.value">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="优惠券状态" required prop="couponStatus">
                        <el-select v-model="form.couponStatus" placeholder="优惠券状态">
                            <el-option v-for="item in query.couponStatusOptions" :key="item.value" :label="item.label"
                                :value="item.value">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="类型,1：满减券；2：运费券；" required prop="couponType">
                        <el-select v-model="form.couponType" placeholder="类型,1：满减券；2：运费券；">
                            <el-option v-for="item in query.options" :key="item.value" :label="item.label"
                                :value="item.value">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="用券-结束时间" required prop="couponUseEndDate">
                        <el-date-picker v-model="form.couponUseEndDate" type="date" placeholder="用券-结束时间">
                        </el-date-picker>
                    </el-form-item>
                    <el-form-item label="用券-效期,x天h时m分s秒，如：1天0时0分" required prop="couponUseExpirationTimestamp">
                        <el-input v-model="form.couponUseExpirationTimestamp" placeholder="用券-效期,x天h时m分s秒，如：1天0时0分"
                            auto-complete="off" />
                    </el-form-item>
                    <el-form-item label="用券-开始时间" required prop="couponUseStartDate">
                        <el-date-picker v-model="form.couponUseStartDate" type="date" placeholder="用券-开始时间">
                        </el-date-picker>
                    </el-form-item>
                    <el-form-item label="用券方式,1：固定时间；2：领取后生效" required prop="couponUseType">
                        <el-select v-model="form.couponUseType" placeholder="用券方式,1：固定时间；2：领取后生效">
                            <el-option v-for="item in query.couponUseTypeoptions" :key="item.value" :label="item.label"
                                :value="item.value">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="已发放张数, 系统默认值=0" required prop="couponUsed">
                        <el-select v-model="form.couponUsed" placeholder="已发放张数, 系统默认值=0">
                            <el-option v-for="item in query.options" :key="item.value" :label="item.label"
                                :value="item.value">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="每人限领张数,0表示无限制，最大值=发放张数;" required prop="couponUserNum">
                        <el-select v-model="form.couponUserNum" placeholder="每人限领张数,0表示无限制，最大值=发放张数;">
                            <el-option v-for="item in query.options" :key="item.value" :label="item.label"
                                :value="item.value">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="优惠券使用用户类型" required prop="couponUserType">
                        <el-select v-model="form.couponUserType" placeholder="优惠券使用用户类型">
                            <el-option v-for="item in query.couponUserTypeOptions" :key="item.value" :label="item.label"
                                :value="item.value">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="优惠券使用范围：1. 平台通用类；2. 店铺通用类;3. 品类通用类4. SKU使用类" required prop="couponUsingRange">
                        <el-select v-model="form.couponUsingRange"
                            placeholder="优惠券使用范围：1. 平台通用类；2. 店铺通用类;3. 品类通用类4. SKU使用类">
                            <el-option v-for="item in query.couponUsingRangeOptions" :key="item.value" :label="item.label"
                                :value="item.value">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="创建者" required prop="createBy">
                        <el-input v-model="form.createBy" placeholder="创建者" auto-complete="off" />
                    </el-form-item>
                    <el-form-item label="创建时间" required prop="createDate">
                        <el-date-picker v-model="form.createDate" type="date" placeholder="创建时间">
                        </el-date-picker>
                    </el-form-item>
                    <el-form-item label="" required prop="features">
                        <el-input v-model="form.features" placeholder="" auto-complete="off" />
                    </el-form-item>
                    <el-form-item label="" required prop="id">
                        <el-input v-model="form.id" placeholder="" auto-complete="off" />
                    </el-form-item>
                    <el-form-item label="所属组织" required prop="organId">
                        <el-input v-model="form.organId" placeholder="所属组织" auto-complete="off" />
                    </el-form-item>
                    <el-form-item label="组织路径" required prop="organPath">
                        <el-input v-model="form.organPath" placeholder="组织路径" auto-complete="off" />
                    </el-form-item>

                    <el-form-item label="" required prop="parentId">
                        <el-input v-model="form.parentId" placeholder="" auto-complete="off" />
                    </el-form-item>
                    <el-form-item label="平台类型: 参考枚举-客户端类型" required prop="platformId">
                        <el-select v-model="form.platformId" placeholder="平台类型: 参考枚举-客户端类型">
                            <el-option v-for="item in query.options" :key="item.value" :label="item.label"
                                :value="item.value">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="拒绝原因" required prop="rejectReason">
                        <el-input v-model="form.rejectReason" placeholder="拒绝原因" auto-complete="off" />
                    </el-form-item>
                    <el-form-item label="" required prop="remark">
                        <el-input v-model="form.remark" placeholder="" auto-complete="off" />
                    </el-form-item>
                    <el-form-item label="审核人" required prop="reviewer">
                        <el-input v-model="form.reviewer" placeholder="审核人" auto-complete="off" />
                    </el-form-item>
                    <el-form-item label="排序" required prop="sorted">
                        <el-select v-model="form.sorted" placeholder="排序">
                            <el-option v-for="item in query.options" :key="item.value" :label="item.label"
                                :value="item.value">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="状态" required prop="status">
                        <el-select v-model="form.status" placeholder="状态">
                            <el-option v-for="item in query.options" :key="item.value" :label="item.label"
                                :value="item.value">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="应用于店铺" required prop="storeId">
                        <el-input v-model="form.storeId" placeholder="应用于店铺" auto-complete="off" />
                    </el-form-item>
                    <el-form-item label="所属租户" required prop="tenantId">
                        <el-input v-model="form.tenantId" placeholder="所属租户" auto-complete="off" />
                    </el-form-item>
                    <el-form-item label="更新者" required prop="updateBy">
                        <el-input v-model="form.updateBy" placeholder="更新者" auto-complete="off" />
                    </el-form-item>
                    <el-form-item label="更新时间" required prop="updateDate">
                        <el-date-picker v-model="form.updateDate" type="date" placeholder="更新时间">
                        </el-date-picker>
                    </el-form-item>
                </el-form>

                <div slot="footer" class="dialog-footer">
                    <el-button @click="dialogFormVisible = false">取 消</el-button>
                    <el-button type="primary" @click="handleEditSave">确 定</el-button>
                </div>
            </el-dialog>
        </div>

        <div class="block">
            <el-drawer title="发布优惠券" size="68%" :visible.sync="drawer" :direction="direction" :before-close="handleClose">
                <div class="app-container">

                    <el-steps :active="step" align-center>
                        <el-step title="优惠券基础信息"></el-step>
                        <el-step title="优惠券使用范围"></el-step>
                    </el-steps>

                    <el-form :model="form" :rules="CouponRules" ref="CouponForm">
                        <div v-show="this.step == 1">
                            <el-row>
                                <el-col :span="12">
                                    <el-form-item label="优惠券名称" required prop="couponName">
                                        <el-input v-model="form.couponName" style="width:90%" placeholder="优惠券名称，如：满100减20"
                                            auto-complete="off" />
                                    </el-form-item>
                                </el-col>

                                <el-col :span="12">
                                    <el-form-item label="优惠券描述" required prop="couponInfo">
                                        <el-input v-model="form.couponInfo" placeholder="优惠券描述" style="width:100%"
                                            auto-complete="off" />
                                    </el-form-item>
                                </el-col>
                            </el-row>

                            <el-row>
                                <el-col :span="12">
                                    <el-form-item v-has="'PLATFROM_TENANT_QUERY'" prop="tenantId" label="所属租户"
                                        placeholder="所属租户" required>
                                        <el-cascader v-model="form.tenantId" style="width:90%" ref="tenantTreeForm"
                                            placeholder="所属租户" :props="tenantProps" :options="tenantOptions"
                                            @change="chooseTenantForm" clearable collapse-tags filterable></el-cascader>
                                    </el-form-item>
                                </el-col>

                                <el-col :span="12">
                                    <el-form-item required prop="organId" label="所属组织">
                                        <el-cascader v-model="form.organId" ref="organTreeForm" placeholder="所属组织"
                                            :props="props" :options="organFormOptions" @change="chooseOrganForm(form)"
                                            style="width:100%" collapse-tags filterable></el-cascader>
                                    </el-form-item></el-col>
                            </el-row>
                            <el-row>
                                <el-col :span="12">

                                    <el-form-item label="优惠券使用用户类型" required prop="couponUserType">
                                        <el-select v-model="form.couponUserType" style="width:90%" placeholder="优惠券使用用户类型">
                                            <el-option v-for="item in query.couponUserTypeOptions" :key="item.value"
                                                :label="item.label" :value="item.value">
                                            </el-option>
                                        </el-select>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="12">
                                    <el-form-item label="成本分摊" required prop="costAllocation">
                                        <el-select v-model="form.costAllocation" style="width:100%" placeholder="成本分摊">
                                            <el-option v-for="item in query.couponCostAllocationOptions" :key="item.value"
                                                :label="item.label" :value="item.value">
                                            </el-option>
                                        </el-select>
                                    </el-form-item>
                                </el-col>
                            </el-row>

                            <el-row>
                                <el-col :span="12">
                                    <el-form-item label="优惠券面额" required prop="couponAmount">
                                        <el-input v-model="form.couponAmount" style="width:90%"
                                            placeholder="优惠券面额"></el-input>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="12">
                                    <el-form-item label="优惠最高金额" required prop="couponMaxAmount">
                                        <el-input v-model="form.couponMaxAmount" style="width:100%"
                                            placeholder="优惠最高金额"></el-input>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <el-row>
                                <el-col :span="12">
                                    <el-form-item label="适用渠道" required prop="couponApplySource">
                                        <el-select v-model="form.couponApplySource" style="width:90%" placeholder="适用渠道">
                                            <el-option v-for="item in query.couponApplySourceOptions" :key="item.value"
                                                :label="item.label" :value="item.value">
                                            </el-option>
                                        </el-select>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="12">
                                    <el-form-item label="类型" required prop="couponType">
                                        <el-select v-model="form.couponType" style="width:100%" placeholder="类型">
                                            <el-option v-for="item in query.couponTypeOptions" :key="item.value"
                                                :label="item.label" :value="item.value">
                                            </el-option>
                                        </el-select>
                                    </el-form-item>

                                </el-col>
                            </el-row>

                            <el-row>
                                <el-col :span="12">
                                    <el-form-item label="领券/发券时间-开始时间" required prop="couponGiveStartDate">
                                        <el-date-picker v-model="form.couponGiveStartDate"
                                            value-format="yyyy-MM-dd mm:HH:ss" format="yyyy-MM-dd mm:HH:ss"
                                            style="width:90%" type="date" placeholder="领券/发券时间-开始时间">
                                        </el-date-picker>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="12">
                                    <el-form-item label="领券/发券时间-结束时间" required prop="couponGiveEndDate">
                                        <el-date-picker v-model="form.couponGiveEndDate" value-format="yyyy-MM-dd mm:HH:ss"
                                            format="yyyy-MM-dd mm:HH:ss" style="width:100%" type="date"
                                            placeholder="领券/发券时间-结束时间">
                                        </el-date-picker>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <el-row>
                                <el-col :span="12">

                                    <el-form-item label="每人限领张数" required prop="couponUserNum">
                                        <el-input v-model="form.couponUserNum" style="width:90%" placeholder="每人限领张数"
                                            auto-complete="off" />
                                    </el-form-item>


                                </el-col>

                                <el-col :span="12">
                                    <el-form-item label="用券方式" required prop="couponUseType">
                                        <el-select v-model="form.couponUseType" style="width:100%"
                                            @change="handleChooseCouponUseType" placeholder="用券方式">
                                            <el-option v-for="item in query.couponUseTypeOptions" :key="item.value"
                                                :label="item.label" :value="item.value">
                                            </el-option>
                                        </el-select>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <el-row>
                                <el-col :span="12">
                                    <el-form-item v-if="form.couponUseType == 1" label="用券-开始时间" required
                                        prop="couponUseStartDate">
                                        <el-date-picker v-model="form.couponUseStartDate" value-format="yyyy-MM-dd mm:HH:ss"
                                            format="yyyy-MM-dd mm:HH:ss" style="width:90%" type="date"
                                            placeholder="用券-开始时间">
                                        </el-date-picker>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="12">
                                    <el-form-item v-if="form.couponUseType == 1" label="用券-结束时间" required
                                        prop="couponUseEndDate">
                                        <el-date-picker v-model="form.couponUseEndDate" value-format="yyyy-MM-dd mm:HH:ss"
                                            format="yyyy-MM-dd mm:HH:ss" style="width:100%" type="date"
                                            placeholder="用券-结束时间">
                                        </el-date-picker>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <el-row>
                                <el-col :span="12">

                                    <el-form-item label="优惠券状态" required prop="couponStatus">
                                        <el-select v-model="form.couponStatus" style="width:90%" placeholder="优惠券状态">
                                            <el-option v-for="item in query.couponStatusOptions" :key="item.value"
                                                :label="item.label" :value="item.value">
                                            </el-option>
                                        </el-select>
                                    </el-form-item>
                                </el-col>

                                <el-col :span="12">
                                    <el-form-item label="状态" required prop="status">
                                        <el-select v-model="form.status" style="width:100%" placeholder="状态">
                                            <el-option v-for="item in query.bizEntityOptions" :key="item.value"
                                                :label="item.label" :value="item.value">
                                            </el-option>
                                        </el-select>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <el-row>
                                <el-col :span="12">
                                    <el-form-item label="满足金额" required prop="meetPrice">
                                        <el-input v-model="form.meetPrice" style="width:90%" placeholder="满足金额"
                                            auto-complete="off" />
                                    </el-form-item>
                                </el-col>
                                <el-col :span="12">
                                    <el-form-item label="抵扣金额" required prop="couponDiscount">
                                        <el-input v-model="form.couponDiscount" style="width:100%" placeholder="抵扣金额"
                                            auto-complete="off" />
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <el-row>
                                <el-col :span="12">
                                    <el-form-item label="已发放张数" required prop="couponUsed">
                                        <el-input v-model="form.couponUsed" style="width:90%" disabled placeholder="已发放张数"
                                            auto-complete="off" />
                                    </el-form-item>
                                </el-col>
                                <el-col :span="12">
                                    <el-form-item label="发放张数" required prop="couponNum">
                                        <el-input v-model="form.couponNum" style="width:100%"
                                            placeholder="发放张数,最大值=100000，修改优惠券总量时只能增加不能减少，请谨慎设置;"></el-input>
                                    </el-form-item>

                                </el-col>
                            </el-row>

                            <el-row>
                                <el-col :span="12">
                                    <el-form-item label="备注" required prop="remark">
                                        <el-input type="textarea" v-model="form.remark" style="width:80%" placeholder="备注"
                                            auto-complete="off" />
                                    </el-form-item>
                                </el-col>
                                <el-col :span="12">

                                    <el-form-item label="平台类型" required prop="platformId">
                                        <el-select v-model="form.platformId" style="width:100%"
                                            placeholder="平台类型: 参考枚举-客户端类型">
                                            <el-option v-for="item in query.options" :key="item.value" :label="item.label"
                                                :value="item.value">
                                            </el-option>
                                        </el-select>
                                    </el-form-item>
                                </el-col>
                            </el-row>

                            <el-row>

                                <el-col :span="12">
                                    <el-form-item label="审核人" required prop="reviewer">
                                        <el-input v-model="form.reviewer" style="width:90%" placeholder="审核人"
                                            auto-complete="off" />
                                    </el-form-item>
                                </el-col>
                                <el-col :span="12">
                                    <el-form-item v-if="form.couponUseType == 2" label="用券-效期" required
                                        prop="couponUseExpirationTimestamp">
                                        <el-input v-model="form.couponUseExpirationTimestamp" style="width:100%"
                                            placeholder="用券-效期,x天h时m分s秒，如：1天0时0分" auto-complete="off" />
                                    </el-form-item>

                                </el-col>
                            </el-row>
                            <el-row>
                                <el-col :span="12">
                                    <el-form-item label="使用门槛" required prop="couponCondition">
                                        <el-input v-model="form.couponCondition" style="width:90%" placeholder="使用门槛"
                                            auto-complete="off" />
                                    </el-form-item>
                                </el-col>

                                <el-col :span="12">
                                    <el-form-item label="优惠券规则" hidden required prop="rule">
                                        <el-input v-model="form.ruleId" suffix-icon="el-icon-date"
                                            @focus="handleQueryDroolsRuleList(form)" placeholder="优惠券规则" style="width:90%"
                                            auto-complete="off" />
                                    </el-form-item>
                                    <el-form-item label="优惠券规则" required prop="ruleName">
                                        <el-input v-model="form.ruleName" suffix-icon="el-icon-date"
                                            @focus="handleQueryDroolsRuleList(form)" placeholder="促销规则" style="width:90%"
                                            auto-complete="off" />
                                    </el-form-item>
                                </el-col>
                            </el-row>

                        </div>
                        <div v-show="this.step == 2">
                            <el-row>
                                <el-col :span="12">
                                    <el-form-item label="优惠券使用范围" required prop="couponUsingRange">
                                        <el-select v-model="form.couponUsingRange" style="width:90%"
                                            @change="handleChageUsingRange" placeholder="优惠券使用范围">
                                            <el-option v-for="item in query.couponUsingRangeOptions" :key="item.value"
                                                :label="item.label" :value="item.value">
                                            </el-option>
                                        </el-select>
                                    </el-form-item>
                                </el-col>

                                <el-col :span="12">
                                    <el-form-item label="应用于店铺" required prop="storeId">
                                        <el-cascader v-model="form.storeId" ref="storeForm"
                                            :disabled="form.couponUsingRange <= 1" style="width:100%" placeholder="应用于店铺"
                                            :props="storeProps" :options="storeOptions" @change="chooseStoreForm(form)"
                                            clearable collapse-tags filterable></el-cascader>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <el-row>
                                <el-col :span="12">
                                    <el-form-item required prop="goodsTypeIds" label="促销商品类型">
                                        <el-cascader v-model="form.goodsTypeIds" :disabled="form.couponUsingRange <= 2"
                                            ref="goodsTypeTreeForm" style="width:90%" placeholder="促销商品类型"
                                            :props="goodsTypeTreeProps" :options="goodsTypeTreeOptions"
                                            @change="getGoodsType(form)" collapse-tags filterable></el-cascader>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="12">
                                    <el-form-item label="促销商品" required prop="productIds">
                                        <el-cascader v-model="form.productIds" :disabled="form.couponUsingRange <= 2"
                                            ref="goodsForm" style="width:100%" placeholder="促销商品" :props="goodsProps"
                                            :options="goodsOptions" @change="getGoods(form)" collapse-tags
                                            filterable></el-cascader>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                        </div>
                        <div v-show="form.couponUsingRange == 4" class="Search">
                            <el-form-item>
                                <el-button v-has="'GOODS_ADD'" type="success" icon="el-icon-plus" size="mini"
                                    @click="handleShowQuerySku">选择优惠券SKU</el-button>
                            </el-form-item>
                        </div>

                        <div v-show="this.step == 2 && form.couponUsingRange >= 2" class="block">
                            <el-table v-loading="listLoading" :data="couponUsingRangeList" element-loading-text="Loading"
                                border fit highlight-current-row>

                                <el-table-column type="selection" width="45">
                                </el-table-column>
                                <el-table-column v-if="form.couponUsingRange == 4" label="SKU" width="110">
                                    <template slot-scope="scope">
                                        {{ scope.row.skuName }}
                                    </template>
                                </el-table-column>
                                <el-table-column v-if="form.couponUsingRange == 4" label="SKU编码" width="110">
                                    <template slot-scope="scope">
                                        {{ scope.row.goodsCode }}
                                    </template>
                                </el-table-column>
                                <el-table-column v-if="form.couponUsingRange == 3" label="优惠券适用商品名称" width="110">
                                    <template slot-scope="scope">
                                        {{ scope.row.goodsName }}
                                    </template>
                                </el-table-column>
                                <el-table-column v-if="form.couponUsingRange == 3" label="优惠券适用商品编号" width="110">
                                    <template slot-scope="scope">
                                        {{ scope.row.goodsId }}
                                    </template>
                                </el-table-column>
                                <el-table-column v-if="form.couponUsingRange == 2" label="应用于商铺" width="110">
                                    <template slot-scope="scope">
                                        {{ scope.row.storeId }}
                                    </template>
                                </el-table-column>
                                <el-table-column v-if="this.form.type === '1'" label="折扣价格" width="110">
                                    <template slot-scope="scope">
                                        {{ scope.row.discountPrice }}
                                    </template>
                                </el-table-column>

                                <el-table-column label="备注">
                                    <template slot-scope="scope">
                                        {{ scope.row.remark }}
                                    </template>
                                </el-table-column>

                                <el-table-column align="center" fixed="right" prop="created_at" label="操作" width="200">
                                    <template slot-scope="scope">
                                        <el-link type="warning" icon="el-icon-edit"
                                            @click="handleEdit(scope.$index, scope.row)">
                                            修改
                                        </el-link>
                                        <el-link type="danger" icon="el-icon-plus"
                                            @click="handleDelete(scope.$index, scope.row)">
                                            删除
                                        </el-link>
                                    </template>
                                </el-table-column>
                            </el-table>
                        </div>

                    </el-form>

                </div>

                <div class="bottom clearfix">
                    <el-button v-if="showPrevStepBtn" style="float: right;" type="primary"
                        @click="handleGoTOPreStep">上一步</el-button>
                    <el-button v-if="showNextStepBtn" style="float: right;" type="primary"
                        @click="handleGoTONextStep">下一步</el-button>
                    <el-button style="float: right;" type="primary" @click="handleSubmitCoupon">提交</el-button>
                </div>
            </el-drawer>
        </div>

        <div class="block">
            <!--弹框-->
            <el-dialog title="请选择需要促销的SKU" width="58%" :visible.sync="dialogSkuListVisible">
                <el-table v-loading="listLoading" :data="skuList" element-loading-text="Loading"
                    @selection-change="handleSelectionChange" border fit highlight-current-row>

                    <el-table-column type="selection" fixed="left" width="45">
                    </el-table-column>
                    <el-table-column label="SKU" width="110">
                        <template slot-scope="scope">
                            {{ scope.row.goodsName }}
                        </template>
                    </el-table-column>
                    <el-table-column label="SKU编码" width="110">
                        <template slot-scope="scope">
                            {{ scope.row.goodsCode }}
                        </template>
                    </el-table-column>
                    <el-table-column label="关联商品" width="110">
                        <template slot-scope="scope">
                            {{ scope.row.goodsId }}
                        </template>
                    </el-table-column>
                    <el-table-column label="库存" width="110">
                        <template slot-scope="scope">
                            {{ scope.row.stockCount }}
                        </template>
                    </el-table-column>
                    <el-table-column label="价格(单位：分)" width="110">
                        <template slot-scope="scope">
                            {{ scope.row.price }}
                        </template>
                    </el-table-column>
                    <el-table-column label="备注" width="110">
                        <template slot-scope="scope">
                            {{ scope.row.remark }}
                        </template>
                    </el-table-column>
                </el-table>

                <div class="block">
                    <el-pagination :current-page="this.query.sku.currentPage" :page-sizes="[10, 20, 30, 40]"
                        :page-size="this.query.sku.pageSize" layout="total, sizes, prev, pager, next, jumper"
                        :total="this.query.sku.total" @size-change="handleSkuSizeChange"
                        @current-change="handleSkuCurrentChange" />
                </div>

                <div slot="footer" class="dialog-footer">
                    <el-button @click="dialogSkuListVisible = false">取 消</el-button>
                    <el-button type="primary" @click="handleChooseSkuComfirm">确 定</el-button>
                </div>
            </el-dialog>

        </div>

        <div class="block">
            <!--弹框-->
            <el-dialog title="选择促销活动规则" :visible.sync="dialogRuleChooseVisible">
                <div class="block">
                    <el-table v-loading="listLoading" :data="droolsRuleList" element-loading-text="Loading" border fit
                        highlight-current-row>
                        <el-table-column align="center" label="选择" width="35">
                            <template slot-scope="scope">
                                <el-radio :label="scope.row.id" v-model="form.rule"
                                    @change="handelChooseDroolsRule(scope.row)">&nbsp;&nbsp;</el-radio>
                            </template>
                        </el-table-column>
                        <el-table-column label="模板名称" width="210">
                            <template slot-scope="scope">
                                {{ scope.row.templateName }}
                            </template>
                        </el-table-column>
                        <el-table-column label="状态" width="110">
                            <template slot-scope="scope">
                                {{ scope.row.status }}
                            </template>
                        </el-table-column>
                        <el-table-column label="备注">
                            <template slot-scope="scope">
                                {{ scope.row.remark }}
                            </template>
                        </el-table-column>
                    </el-table>
                </div>

            </el-dialog>
        </div>
    </div>
</template>

<script>
import { getList, doSubmit, queryForList, doDelete, doEdit } from '@/api/Coupon/'
import { getList as queryDroolsRulePage } from '@/api/DroolsRules/'
import { getStoreList as queryStore } from '@/api/Store/'
import { queryForList as queryGoods } from '@/api/Goods/'
import { getList as querySkuPage } from '@/api/GoodsPrice/'

import { getList as getOrganPager, querySubordinateOrgan, queryAssigneeOrgan } from '@/api/Organ'
import { getOrganId, getTenantId, getOrganPath, getCurrentLoginUserId, handleBuildTreeDataStruct, getSelectedNodes, getSelectedNodesObject, getSystemErrorCode, getCurrentTimeYYYY_MM_dd_HH_mm_ss } from '@/api/utils'
import { getTenantList, getToken, getOrganList, getDictionary, translateOrganId2OrganName } from '@/utils/auth'
import { getGoodsTypeTree } from '@/api/GoodsType/'

import localStorageUtil from '@/utils/localStorageUtil'
import sessionStorageUtil from '@/utils/sessionStorageUtil'

export default {
    data() {
        return {
            list: null,
            listLoading: true,
            total: 5,
            currentPage: 1,
            pageSize: 10,
            direction: 'rtl',
            drawer: false,
            step: 1,
            showNextStepBtn: true,
            showPrevStepBtn: false,
            showSubmitBtn: false,
            dialogFormVisible: false,
            addForVisible: false,
            showAdvenceQuery: false,
            couponUsingRangeList: [],
            dialogSkuListVisible: false,
            dialogRuleChooseVisible: false,
            skuList: [],
            droolsRuleList: [],
            form: {
                costAllocation: '',
                couponAmount: '',
                couponApplySource: '',
                couponCondition: '',
                couponDiscount: '',
                couponGiveEndDate: '',
                couponGiveStartDate: '',
                couponInfo: '',
                couponMaxAmount: '',
                couponName: '',
                couponNum: '',
                couponStatus: '',
                couponType: '',
                couponUseEndDate: '',
                couponUseExpirationTimestamp: '',
                couponUseStartDate: '',
                couponUseType: '',
                couponUsed: '',
                couponUserNum: '',
                couponUserType: '',
                couponUsingRange: '',
                createBy: '',
                createDate: '',
                features: '',
                id: '',
                ruleId: '',
                isDeleted: '',
                meetPrice: '',
                organId: '',
                organPath: '',
                parentId: '',
                platformId: '',
                rejectReason: '',
                remark: '',
                reviewer: '',
                sorted: '',
                status: '',
                storeId: '',
                tenantId: '',
                updateBy: '',
                updateDate: ''
            },
            couponUsingRange: {
                couponId: '', //优惠券编号,
                couponType: '', //,使用范围类型
                createBy: '', //创建者,
                createDate: '', //创建时间,
                features: '', //,
                id: '', //,
                isDeleted: '', //,
                organId: '', //所属组织,
                organPath: '', //组织路径,
                parentId: '', //,
                remark: '', //,
                sorted: '', //排序,
                status: '', //状态,
                tenantId: '', //所属租户,
                updateBy: '', //更新者,
                updateDate: '', //更新时间,
                usingRangeId: '' //使用范围编号,根据优惠券类型不同，此字段含义不同：1. 全平台优惠券：本字段为空2. 店铺优惠券：本字段为店铺编号3. 类目优惠券：本字段为类目编号4. SKU优惠券：本字段为SKU编号,
            },
            // 新增or编辑弹框标题(根据点击的新增or编辑按钮显示不同的标题)
            titleMap: {
                add: '新增',
                edit: '编辑'
            },
            CouponRules: {
                features: [{ required: true, message: '不能为空!', trigger: 'blur' }]
            },
            props: { // 配置项（必选）
                value: 'id',
                label: 'organName',
                children: 'children',
                title: '组织',
                multiple: false,
                checkStrictly: true,
                emitPath: false
                // disabled:true
            },
            organTreeProps: {
                children: 'children',
                label: 'organName',
                url: 'url'
            },
            tenantProps: { // 配置项（必选）
                value: 'id',
                label: 'organName',
                children: 'children',
                title: '租户',
                multiple: false,
                checkStrictly: true,
                emitPath: false
                // disabled:true
            },
            goodsTypeTreeProps: { // 配置项（必选）
                value: 'id',
                label: 'typeName',
                children: 'children',
                checkStrictly: true,
                title: '商品类型',
                multiple: true,
                checkStrictly: true,
                emitPath: false
                // disabled:true
            },
            goodsProps: { // 配置项（必选）
                value: 'id',
                label: 'goodsName',
                children: 'children',
                checkStrictly: true,
                title: '商品',
                multiple: true,
                checkStrictly: true,
                emitPath: false
                // disabled:true
            },
            storeProps: { // 配置项（必选）
                value: 'id',
                label: 'storeName',
                children: 'children',
                checkStrictly: true,
                title: '店铺',
                multiple: false,
                checkStrictly: true,
                emitPath: false
                // disabled:true
            },
            goodsTypeTreeOptions: [{ id: '1', parentId: '2', typeName: '商品', children: [] }],
            organFormOptions: [{ id: '1', parentId: '2', organName: '平台', children: [] }],
            tenantOptions: [{ id: '1', parentId: '2', organName: '平台', children: [] }],
            organOptions: [{ id: '1', parentId: '2', organName: '平台', children: [] }],
            supplierOptions: [{ id: '1', parentId: '2', organName: '平台', children: [] }],
            storeOptions: [{ id: '1', parentId: '2', storeName: '店铺', children: [] }],
            skuOptions: [{ id: '1', parentId: '2', organName: '平台', children: [] }],
            goodsOptions: [{ id: '1', parentId: '2', organName: '平台', children: [] }],
            organQueryModel: 'selfAndSub',
            supplier: null, //供应商
            goodsType: null, //商品分类
            // // 新增or编辑弹框标题(根据点击的新增or编辑按钮显示不同的标题)
            optionData: [{ id: '1', parentId: '2', organName: '平台', children: [] }],
            // 新增和编辑弹框标题
            dialogStatus: '',
            query: {
                params: {
                    costAllocation: '', //成本分摊 1:平台分摊，2店铺分摊,
                    couponAmount: '', //优惠券面额,
                    couponApplySource: '', //适用渠道,1：全部渠道；2：门店,
                    couponCondition: '', //使用门槛,0表示无门槛，类型="运费券"时，固定无门槛=0；类型="满减券"时，必须>0;,
                    couponDiscount: '', //抵扣金额（面额）,减免金额，必须>0，类型="满减券"时，=<使用门槛;,
                    couponGiveEndDate: '', //领券/发券时间-结束时间,
                    couponGiveStartDate: '', //领券/发券时间-开始时间,
                    couponInfo: '', //描述,
                    couponMaxAmount: '', //优惠最高金额,
                    couponName: '', //名称, 满100减20,
                    couponNum: '', //发放张数,最大值=100000，修改优惠券总量时只能增加不能减少，请谨慎设置;,
                    couponStatus: '', //0：草稿；1：启用；2：禁用；3：已过期；,
                    couponType: '', //类型,1：满减券；2：运费券；,
                    couponUseEndDate: '', //用券-结束时间,
                    couponUseExpirationTimestamp: '', //用券-效期,x天h时m分s秒，如：1天0时0分,
                    couponUseStartDate: '', //用券-开始时间,
                    couponUseType: '', //用券方式,1：固定时间；2：领取后生效,
                    couponUsed: '', //已发放张数, 系统默认值=0,
                    couponUserNum: '', //每人限领张数,0表示无限制，最大值=发放张数;,
                    couponUserType: '', //优惠券使用用户类型,
                    couponUsingRange: '', //优惠券使用范围：1. 平台通用类；2. 店铺通用类;3. 品类通用类4. SKU使用类,
                    createBy: '', //创建者,
                    createDate: '', //创建时间,
                    features: '', //,
                    meetPrice: '',
                    ruleId: '',
                    ruleName: '',
                    id: '', //,
                    isDeleted: '', //,
                    meetPrice: '', //优惠券使用需要的满足条件,
                    organId: '', //所属组织,
                    organPath: '', //组织路径,
                    parentId: '', //,
                    platformId: '', //平台类型: 参考枚举-客户端类型,
                    rejectReason: '', //拒绝原因,
                    remark: '', //,
                    reviewer: '', //审核人,
                    sorted: '', //排序,
                    status: '', //状态,
                    storeId: '', //应用于店铺,
                    tenantId: '', //所属租户,
                    updateBy: '', //更新者,
                    updateDate: '', //更新时间,

                },
                goods: {
                    organId: '',
                    organPath: '',
                    tenantId: '',
                    goodsTypeId: '',
                    goodsTypePath: '',
                    goodsName: '',
                    supplierId: '',
                    sys: '',
                    goodsTypeIds: [],
                    organIds: []
                },
                sku: {
                    goodsId: '',
                    goodsIds: [],
                    goodsName: '',
                    goodsCode: '',
                    organId: '',
                    organPath: '',
                    goodsTypeIds: [],
                    total: 5,
                    currentPage: 1,
                    pageSize: 10,
                },
                store: {
                    organIds: [],
                    addressId: '', // 关联地址
                    enable: '', // 是否生效
                    tenantId: '',
                    organId: '', // 所属组织
                    organPath: '', // 组织路径
                    storeName: '', // 店铺名称
                },
                droolsRule: {
                    drlName: '',
                    features: '',
                    organId: '',
                    organPath: '',
                    status: '',
                    templateContent: '',
                    templateName: '',
                    tenantId: '',
                    total: 5,
                    currentPage: 1,
                    pageSize: 10,
                },
                couponUsingRange: {
                    couponId: '', //优惠券编号,
                    couponType: '', //,使用范围类型
                    createBy: '', //创建者,
                    createDate: '', //创建时间,
                    features: '', //,
                    id: '', //,
                    isDeleted: '', //,
                    organId: '', //所属组织,
                    organPath: '', //组织路径,
                    parentId: '', //,
                    remark: '', //,
                    sorted: '', //排序,
                    status: '', //状态,
                    tenantId: '', //所属租户,
                    updateBy: '', //更新者,
                    updateDate: '', //更新时间,
                    usingRangeId: '' //使用范围编号,根据优惠券类型不同，此字段含义不同：1. 全平台优惠券：本字段为空2. 店铺优惠券：本字段为店铺编号3. 类目优惠券：本字段为类目编号4. SKU优惠券：本字段为SKU编号,
                },
                timeValue: '',
                options: [{
                    value: '1',
                    label: '黄金糕'
                }, {
                    value: '2',
                    label: '双皮奶'
                }],
                value: ''
            }
        }
    },
    created() {
        this.initOrganTree(() => {
            this.setOrganQueryType()
        }, getOrganPath())
        this.initGoodsTypeTree()
        this.initOrganFormTree()
        this.handleFiltTenant()
        this.handleCouponCostAllocationOptions()
        this.handleInitOptions("CouponApplySource", "couponApplySourceOptions")
        this.handleInitOptions("CouponType", "couponTypeOptions")
        this.handleInitOptions("CouponUseType", "couponUseTypeOptions")
        this.handleInitOptions("CouponUsingRange", "couponUsingRangeOptions")
        this.handleInitOptions("CouponUserType", "couponUserTypeOptions")
        this.handleInitOptions("CouponStatus", "couponStatusOptions")
        this.handleInitOptions("BizEntity", "bizEntityOptions")


        this.handleQueryStore()



        this.fetchData()
    },
    methods: {
        createOrganQuery() {
            // 选中了租户
            let tenantSelected = this.$refs['tenantTree'] && this.$refs['tenantTree'].getCheckedNodes() || []
            let organSelected = this.$refs['organTree'] && this.$refs['organTree'].getCheckedNodes() || []
            let that = this
            this.query.params.organId = null
            if (tenantSelected.length && tenantSelected[0]) {
                this.query.params.tenantId = tenantSelected[0].data.id
                if (organSelected.length && organSelected[0]) {
                    if (this.props.multiple) {
                        this.query.params.organId = null
                        this.query.params.organIds = []
                        if (organSelected.length && organSelected[0]) {
                            organSelected.forEach(function (element) {
                                that.query.params.organIds.push(element.data.id)
                            })
                        }
                    } else if (organSelected.length && organSelected[0]) {
                        this.query.params.organPath = organSelected[0] && organSelected[0].data.organPath
                    }
                }
            } else {
                this.query.params.tenantId = getTenantId()
                if (organSelected.length && organSelected[0]) {
                    if (this.props.multiple) {
                        this.query.params.organId = null
                        this.query.params.organIds = []
                        if (organSelected.length && organSelected[0]) {
                            organSelected.forEach(function (element) {
                                that.query.params.organIds.push(element.data.id)
                            })
                        }
                    } else if (organSelected.length && organSelected[0]) {
                        this.query.params.organPath = organSelected[0].data.organPath
                    }
                }
            }
            this.query.params.organId = this.query.params.organId || getOrganId()
            this.query.params.organPath = this.query.params.organPath || getOrganPath()
        },
        fetchData() {
            this.listLoading = true
            this.createOrganQuery()
            this.currentPage = this.currentPage || 1
            this.pageSize = this.pageSize || 10
            getList(this.query.params, this.currentPage, this.pageSize).then(response => {
                const data = response.data
                if (!data.entities || typeof data.successful != 'undefined') {
                    this.$message({
                        type: 'info',
                        message: data.description || getSystemErrorCode(data.status)
                    })
                }
                this.total = data.totalCount
                this.pageSize = data.pageSize
                this.currentPage = data.currentPage
                this.list = data.entities
                this.listLoading = false
            })
        },


        handleSizeChange(val) {
            this.currentPage = 1
            this.pageSize = val
            this.fetchData()
        },
        handleCurrentChange(val) {
            this.currentPage = val
            this.fetchData()
        },
        // 新增
        handleAdd() {
            // 显示弹框
            // this.dialogFormVisible = true
            // 新增弹框标题
            this.dialogStatus = 'add'
            this.drawer = true
            this.form.costAllocation = ''
            this.form.couponAmount = ''
            this.form.couponApplySource = ''
            this.form.couponCondition = ''
            this.form.couponDiscount = ''
            this.form.couponGiveEndDate = ''
            this.form.couponGiveStartDate = ''
            this.form.couponInfo = ''
            this.form.couponMaxAmount = ''
            this.form.couponName = ''
            this.form.couponNum = '0'
            this.form.couponStatus = ''
            this.form.couponType = ''
            this.form.couponUseEndDate = ''
            this.form.couponUseExpirationTimestamp = ''
            this.form.couponUseStartDate = ''
            this.form.couponUseType = ''
            this.form.couponUsed = '0'
            this.form.couponUserNum = ''
            this.form.couponUserType = ''
            this.form.couponUsingRange = ''
            this.form.createBy = getCurrentLoginUserId()
            this.form.createDate = getCurrentTimeYYYY_MM_dd_HH_mm_ss()
            this.form.features = ''
            this.form.id = ''
            this.form.isDeleted = ''
            this.form.meetPrice = ''
            this.form.organId = getOrganId()
            this.form.organPath = getOrganPath()
            this.form.parentId = ''
            this.form.platformId = ''
            this.form.rejectReason = ''
            this.form.remark = ''
            this.form.reviewer = ''
            this.form.sorted = ''
            this.form.status = ''
            this.form.storeId = ''
            this.form.tenantId = getTenantId()
            this.form.updateBy = getCurrentLoginUserId()
            this.form.updateDate = getCurrentTimeYYYY_MM_dd_HH_mm_ss()
            this.addForVisible = false
        },
        // 编辑
        handleEdit(index, row) {
            console.log(index)
            console.log(row)
            this.form.costAllocation = row.costAllocation
            this.form.couponAmount = row.couponAmount
            this.form.couponApplySource = row.couponApplySource
            this.form.couponCondition = row.couponCondition
            this.form.couponDiscount = row.couponDiscount
            this.form.couponGiveEndDate = row.couponGiveEndDate
            this.form.couponGiveStartDate = row.couponGiveStartDate
            this.form.couponInfo = row.couponInfo
            this.form.couponMaxAmount = row.couponMaxAmount
            this.form.couponName = row.couponName
            this.form.couponNum = row.couponNum
            this.form.couponStatus = row.couponStatus
            this.form.couponType = row.couponType
            this.form.couponUseEndDate = row.couponUseEndDate
            this.form.couponUseExpirationTimestamp = row.couponUseExpirationTimestamp
            this.form.couponUseStartDate = row.couponUseStartDate
            this.form.couponUseType = row.couponUseType
            this.form.couponUsed = row.couponUsed
            this.form.couponUserNum = row.couponUserNum
            this.form.couponUserType = row.couponUserType
            this.form.couponUsingRange = row.couponUsingRange
            this.form.createBy = row.createBy
            this.form.createDate = row.createDate
            this.form.features = row.features
            this.form.id = row.id
            this.form.isDeleted = row.isDeleted
            this.form.meetPrice = row.meetPrice
            this.form.organId = row.organId
            this.form.organPath = row.organPath
            this.form.parentId = row.parentId
            this.form.platformId = row.platformId
            this.form.rejectReason = row.rejectReason
            this.form.remark = row.remark
            this.form.reviewer = row.reviewer
            this.form.sorted = row.sorted
            this.form.status = row.status
            this.form.storeId = row.storeId
            this.form.tenantId = row.tenantId
            this.form.updateBy = row.updateBy
            this.form.updateDate = row.updateDate
            this.addForVisible = true
            // 显示弹框
            this.dialogFormVisible = true
            // 编辑弹框标题
            this.dialogStatus = 'edit'
        },
        handleDelete(index, row) {
            this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
                center: true
            }).then(() => {
                let couponRemoveVO = { id: row.id, tenantId: row.tenantId }
                doDelete(couponRemoveVO).then(response => {
                    const data = response.data
                    console.log(data)
                    if (data.status == 1) {
                        this.$message({
                            type: 'success',
                            message: '操作成功!'
                        })
                        this.fetchData();
                    } else if (data.status == 403) {
                        this.$message({
                            type: 'warning',
                            message: data.description
                        })
                    } else {
                        this.$message({
                            type: 'info',
                            message: '操作失败!'
                        })
                    }
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                })
            })
        },
        handleEditSave() {

            doEdit(this.form).then(response => {
                const data = response.data
                if (data.status) {
                    this.$message({
                        type: 'success',
                        message: '操作成功!'
                    })
                    this.fetchData();
                } else {
                    this.$message({
                        type: 'info',
                        message: '操作失败!'
                    })
                }
                this.listLoading = false
                this.dialogFormVisible = false
            })
        },
        handleReset() {
            this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
                center: true
            }).then(() => {
                this.$message({
                    type: 'success',
                    message: '删除成功!'
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                })
            })
        },
        initOrganTree(fn, filter, that) {
            this.listLoading = true
            let data = getOrganList()
            if (filter) {
                data = data.filter(function (item, index) {
                    return item.organPath.indexOf(filter) >= 0
                })
            }
            if (that && that.organFormOptions && that.organFormOptions.length >= 0) {
                that.organFormOptions = handleBuildTreeDataStruct(data, 'id', 'parentId')
            } else {
                this.organOptions = handleBuildTreeDataStruct(data, 'id', 'parentId')
            }
            this.listLoading = false
            if (fn) {
                fn()
            }
        },
        initOrganFormTree(fn, filter) {
            this.initOrganTree(fn, filter, this)
        },
        setOrganQueryType() {
            this.organQueryModelChange(localStorageUtil.fetch("SYSTEM_SETTING_ORGAN_QUERY_MODEL"))
        },
        selectOrgan(nodes) {
            let nodesInfo = this.$refs['organTree'].getCheckedNodes()
            let that = this
            this.query.params.organPath = ''
            this.query.params.organIds = []
            if (this.props.multiple) {
                this.query.params.organIds = []
                if (nodesInfo.length) {
                    nodesInfo.forEach(function (element) {
                        that.query.params.organIds.push(element.data.id)
                    })
                }
            } else if (nodesInfo.length) {
                this.query.params.organPath = nodesInfo[0].data.organPath
            }
        },
        selectTenant(nodes) {
            debugger
            let nodesInfo = this.$refs['tenantTree'].getCheckedNodes()
            let that = this
            this.query.params.tenantId = ''
            this.query.params.tenantIds = []
            this.query.params.organId = null
            this.query.params.organIds = []
            this.query.params.organPath = null

            if (this.tenantProps.multiple) {
                this.query.params.tenantIds = []
                if (nodesInfo.length) {
                    nodesInfo.forEach(function (element) {
                        that.query.params.tenantIds.push(element.data.id)
                    })
                }
            } else if (nodesInfo.length) {
                this.query.params.tenantId = nodesInfo[0].data.id
                this.initOrganTree(function () {
                    that.setOrganQueryType()
                }, nodesInfo[0].data.organPath)
            } else {
                this.initOrganTree(function () {
                    that.setOrganQueryType()
                })
            }
        },
        chooseTenantForm(nodes) {
            let nodesInfo = this.$refs['tenantTreeForm'].getCheckedNodes()
            this.form.tenantId = null
            let that = this
            if (nodesInfo.length) {
                this.form.tenantId = nodesInfo[0].data.id
                this.initOrganFormTree(function () {
                    that.setOrganQueryType()
                }, nodesInfo[0].data.organPath, this.organFormOptions)
            } else {
                this.initOrganFormTree(function () {
                    that.setOrganQueryType()
                }, null, this.organFormOptions)
            }
        },
        organQueryModelChange(organQueryModel) {
            if (organQueryModel == 'selected') {
                this.props.multiple = true
            } else if (organQueryModel == 'selfAndSub') {
                this.props.multiple = false
            }
        },
        chooseOrganForm(form) {
            let nodesInfo = this.$refs['organTreeForm'].getCheckedNodes()
            if (nodesInfo[0]) {
                form.organPath = nodesInfo[0].data.organPath
                form.organId = nodesInfo[0].data.id
                form.tenantId = nodesInfo[0].data.tenantId
            }
        },
        handleFiltTenant() {
            let data = getOrganList()
            this.tenantOptions = data.filter(function (item, index) {
                return item.type == 0 || item.type == 1;
            })
        },
        handleInitOptions(bigType, optionsName) {
            const datas = getDictionary()
            let data = datas.filter(function (item, index) {
                return item.bigType === bigType && item.parentId != '0';
            })
            let options = [];
            for (let i = 0; i < data.length; i++) {

                let option = {}
                option['value'] = data[i].value
                option['label'] = data[i].desc
                options.push(option)
            }
            this.query[optionsName] = options
        },
        handleCouponCostAllocationOptions() {
            this.handleInitOptions("CouponCostAllocation", "couponCostAllocationOptions")
        },
        handleQueryStore() {
            this.query.store.organId = getOrganId()
            this.query.store.organPath = getOrganPath()
            this.query.store.tenantId = getTenantId()
            queryStore(this.query.store).then(response => {
                const gateway = response.data
                if (gateway.successful) {
                    this.$message({
                        type: 'success',
                        message: '操作成功!'
                    })
                    this.storeOptions = gateway.data

                } else {
                    this.$message({
                        type: 'info',
                        message: gateway.description || getSystemErrorCode(gateway.status)
                    })
                }
            })
        },
        chooseStoreForm(form) {
            let nodesInfo = this.$refs['storeForm'].getCheckedNodes()
            if (this.storeProps.multiple) {
                let storeIds = []
                if (nodesInfo && nodesInfo.length >= 1) {
                    for (let i = 0; i < nodesInfo.length; i++) {
                        storeIds.push(nodesInfo[i].data.id)
                    }
                    form.storeIds = storeIds
                }
            } else {
                form.storeId = nodesInfo[0].data.id
                this.couponUsingRangeList = []
                if (this.form.couponUsingRange == 2) {
                    this.couponUsingRangeList.push({
                        storeId: nodesInfo[0].data.id,
                        usingRangeId: nodesInfo[0].data.id,
                        couponType: this.form.couponUsingRange
                    })
                }
            }
        },
        handleClose(done) {
            this.$confirm('确认关闭[发布优惠券]操作吗？')
                .then(_ => {
                    done();
                })
                .catch(_ => { });
        },
        handleSubmitCoupon() {

            //全平台适用
            if (this.form.couponUsingRange == 1) {

            } else {
                this.form.couponUsingRangeVOList = this.couponUsingRangeList
            }


            doSubmit(this.form).then(response => {
                const gateway = response.data
                if (gateway.successful) {
                    this.$message({
                        type: 'success',
                        message: '操作成功!'
                    })
                    this.fetchData();
                    this.dialogFormVisible = false
                } else {
                    this.$message({
                        type: 'info',
                        message: gateway.description || getSystemErrorCode(gateway.status)
                    })
                }

                this.listLoading = false
                this.dialogFormVisible = false
            })
        },
        handleValidation(form) {
            if (parseInt(this.form.couponAmount) >= parseInt(this.form.couponMaxAmount)) {
                this.$message({
                    type: 'danger',
                    message: '面额不能大于最高面额!'
                })
                return false;
            }
            if (this.form.couponType === '1') {
                if (!this.form.couponCondition) {
                    this.$message({
                        type: 'danger',
                        message: '使用门槛不能为空!'
                    })
                    return false
                }
            }
            return true;
        },
        handleGoTONextStep() {
            debugger
            if (!this.handleValidation()) {
                return;
            }
            if (this.step < 2) {
                this.step = ++this.step

            }
            this.checkPrevStepShow()
            this.checkNextStepShow()
            this.checkSubmitShow()
        },
        checkPrevStepShow() {
            if (this.step > 1) {
                this.showPrevStepBtn = true
            } else {
                this.showPrevStepBtn = false
            }
        },
        checkNextStepShow() {
            if (this.step < 2) {
                this.showNextStepBtn = true
            } else {
                this.showNextStepBtn = false
            }
        },
        checkSubmitShow() {
            if (this.step == 2) {
                this.showSubmitBtn = true
            } else {
                this.showSubmitBtn = false
            }
        },
        handleGoTOPreStep() {
            debugger
            if (this.step > 1) {
                this.step = --this.step
            }
            this.checkPrevStepShow()
            this.checkNextStepShow()
            this.checkSubmitShow()
        },
        handleChooseCouponUseType(useType) {
            console.log(useType)
        },
        handleShowAdvenceQuery() {
            this.showAdvenceQuery = !this.showAdvenceQuery;
        },
        handleChageUsingRange(usingRange) {
            this.couponUsingRangeList = []
            if (usingRange == 1) {
                this.couponUsingRange = {
                    storeId: form.storeId,
                    usingRangeId: form.storeId,
                    couponType: this.form.couponUsingRange,
                }
            }
        },
        initGoodsTypeTree() {
            this.listLoading = true
            getGoodsTypeTree({ organId: getOrganId() }).then(response => {
                console.log(response)
                const data = response.data.data
                this.goodsTypeTreeOptions = handleBuildTreeDataStruct(data, 'id', 'parentId')
                this.listLoading = false
            })
        },
        getGoodsType(form) {
            let nodesInfo = this.$refs['goodsTypeTreeForm'].getCheckedNodes()
            if (this.goodsTypeTreeProps.multiple) {
                let goodsTypeIds = []
                if (nodesInfo && nodesInfo.length >= 1) {
                    for (let i = 0; i < nodesInfo.length; i++) {
                        goodsTypeIds.push(nodesInfo[i].data.id)
                    }
                    form.goodsTypeIds = goodsTypeIds

                    this.handleQueryGoods(form)
                }
            } else {
                form.goodsTypeId = nodesInfo[0].data.id
                form.goodsTypePath = nodesInfo[0].data.typePath
            }
        },
        getGoods(form) {

            let nodesInfo = this.$refs['goodsForm'].getCheckedNodes()
            if (this.goodsProps.multiple) {
                let goodsIds = []
                if (nodesInfo && nodesInfo.length >= 1) {
                    for (let i = 0; i < nodesInfo.length; i++) {
                        goodsIds.push(nodesInfo[i].data.id)
                    }
                    form.productIds = goodsIds
                    if (this.form.couponUsingRange == 3) {
                        this.couponUsingRangeList = []
                        for (let i = 0; i < nodesInfo.length; i++) {
                            this.couponUsingRangeList.push({
                                goodsId: nodesInfo[i].data.id,
                                goodsName: nodesInfo[i].data.goodsName,
                                storeId: form.storeId,
                                usingRangeId: nodesInfo[i].data.id,
                                couponType: this.form.couponUsingRange,
                            })
                        }
                    }
                }
            } else {
                form.goodsId = nodesInfo[0].data.id
            }
        },
        handleQueryGoods(goodsQuery) {

            this.query.goods.organId = getOrganId()
            this.query.goods.tenantId = getTenantId()
            this.query.goods.organPath = getOrganPath()
            this.query.goods.goodsTypeIds = goodsQuery.goodsTypeIds

            queryGoods(this.query.goods).then(response => {
                const gateway = response.data
                if (gateway.successful) {
                    this.$message({
                        type: 'success',
                        message: '操作成功!'
                    })
                    this.goodsOptions = gateway.data
                } else {
                    this.$message({
                        type: 'info',
                        message: gateway.description || getSystemErrorCode(gateway.status)
                    })
                }
            })
        },
        handleQuerySkuPage() {

            this.query.sku.goodsIds = this.form.productIds
            this.query.sku.goodsTypeIds = this.form.goodsTypeIds
            this.query.sku.organId = this.form.organId || getOrganId()
            this.query.sku.organPath = this.form.organPath || getOrganPath()
            this.query.sku.tenantId = this.form.tenantId || getTenantId()

            this.query.sku.pageSize = this.query.sku.pageSize || 20
            this.query.sku.currentPage = this.query.sku.currentPage || 1

            querySkuPage(this.query.sku, this.query.sku.currentPage, this.query.sku.pageSize).then(response => {
                const data = response.data
                this.query.sku.total = data.totalCount
                this.query.sku.pageSize = data.pageSize
                this.query.sku.currentPage = data.currentPage
                this.skuList = data.entities
                this.listLoading = false
            })
        },
        handleSkuSizeChange(val) {
            this.query.sku.currentPage = 1
            this.query.sku.pageSize = val
            this.handleQuerySkuPage()
        },
        handleSkuCurrentChange(val) {
            this.query.sku.currentPage = val
            this.handleQuerySkuPage()
        },
        handleShowQuerySku() {
            this.dialogSkuListVisible = true
            this.handleQuerySkuPage()
        },
        handleSelectionChange(val) {
            this.skuMultipleSelection = val;
        },
        handleChooseSkuComfirm() {
            if (this.skuMultipleSelection.length <= 0) {
                this.$message({
                    type: 'warning',
                    message: '请选择SKU!'
                })
                return;
            }
            this.handleInitCouponUsingRangeForm(this.form)
        },
        handleChooseSku() {
            this.dialogSkuListVisible = false
        },
        handleInitCouponUsingRangeForm(form) {
            let couponUsingRangeList = []
            for (let i = 0; i < this.skuMultipleSelection.length; i++) {
                couponUsingRangeList.push({
                    skuName: this.skuMultipleSelection[i].goodsName,
                    goodsCode: this.skuMultipleSelection[i].goodsCode,
                    usingRangeId: this.skuMultipleSelection[i].id,
                    couponType: this.form.couponUsingRange
                })
            }
            this.couponUsingRangeList = couponUsingRangeList
        },
        handleQueryDroolsRuleList(node) {
            this.dialogRuleChooseVisible = true
            this.query.droolsRule.organPath = node.organPath || getOrganPath()
            this.query.droolsRule.tenantId = node.tenantId || getTenantId()
            this.query.droolsRule.pageSize = this.query.droolsRule.pageSize || 10
            this.query.droolsRule.currentPage = this.query.droolsRule.currentPage || 1
            this.query.droolsRule.type = 2
            queryDroolsRulePage(this.query.droolsRule, this.query.droolsRule.currentPage, this.query.droolsRule.pageSize).then(response => {
                const data = response.data
                this.total = data.totalCount
                this.query.droolsRule.pageSize = data.pageSize
                this.query.droolsRule.currentPage = data.currentPage
                this.droolsRuleList = data.entities
                this.listLoading = false
            })

        },
        handelChooseDroolsRule(droolsRule) {
            this.form.ruleId = droolsRule.id
            this.form.ruleName = droolsRule.templateName
        },
    }
}
</script>
